package com.foxit.setting;

import java.io.*;
import java.net.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.SimpleTimeZone;
import java.text.SimpleDateFormat;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class SetAdana extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
       request.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       try {
          MongoClient mongo = new MongoClient("192.201.108.97", 27017);
          DB db = mongo.getDB("settingdb");
          DBCollection table = db.getCollection("adana");
          //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
          //sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
          String r = request.getParameter("del");
          if(r != null && r.length()!=0) {
             String s=request.getParameter("id");
             table.remove(new BasicDBObject().append("start", r).append("id",s));
             response.sendRedirect("./SetAdana?modify="+r);
             return;
          }
          String d = request.getParameter("modify");
          if(d == null || d.length()==0) {
             out.println("going to setting home");
             response.sendRedirect("./Sethome");
             return;
          }
          String id, grp, log, expr , count, modify_time;
          int i=0;
          Date now = new Date();
          for(i=1;i <= 5;i++) {
             id = request.getParameter("id"+i); 
             if(id != null && id.length() != 0) {
                grp = request.getParameter("grp"+i);
                log = request.getParameter("log"+i);
                expr = request.getParameter("expr"+i);
                count = request.getParameter("count"+i);
                //modify_time = request.getParameter("modify_time"+i);
                BasicDBObject doc = new BasicDBObject("start",d)
                   .append("id",id)
                   .append("grp",grp)
                   .append("log",log)
                   .append("expr",expr)
                   .append("count",count)
                   .append("modify_time",now);
                table.insert(doc);
             }
          }

          out.println("<HTML><head><style>table,th,td { border:1px solid black;border-collapse:collapse; } </style></head><body><FORM action='./SetAdana'><input type='hidden' name='modify' value='"+d+"'><table border='1' style='width:300px'><tr><th colspan=7>FOXIT Setting Tool(廣告組rule分析)</th></tr>");
          out.println("<tr><th>id</th><th>組名</th><th>日誌通道</th><th>expr</th><th>count基數</th><th>modify time</th><th></th></tr>"); 
          BasicDBObject query = new BasicDBObject("start", d);
          DBCursor cursor = table.find(query);
          while (cursor.hasNext()) {
             Adana s = (Adana) JSON.parseObject(cursor.next().toString(),Adana.class);
             out.println("<tr><td>"+s.getId()+"</td><td>"+s.getGrp()+"</td><td>"+s.getLog()+"</td><td>"+s.getExpr()+"</td><td>"+s.getCount()+"</td><td>"+s.getModify_time()+"</td><td><a href='./SetAdana?del="+d+"&id="+s.getId()+"'>刪除</a></td>");
          }
          cursor.close();
          for(i=1;i <= 5;i++) {
             out.println("<tr><td><input type='text' name='id"+i+"'></td>");
             out.println("<td><input type='text' name='grp"+i+"'></td>");
             out.println("<td><input type='text' name='log"+i+"'></td>");
             out.println("<td><input type='text' name='expr"+i+"'></td>");
             out.println("<td><input type='text' name='count"+i+"'></td>");
             out.println("<td></td><td></td></tr>"); 
          }
          out.println("<td colspan=3><input type='submit' value='submit'></td><td colspan=4><a href='./Slist?modify="+d+"'>回setting主選單</a></td></tr></table></form></body></html>");
       //} catch (UnknownHostException e) {
       //} catch (MongoException e) {
       } catch (Exception ex) {
          out.println(ex.getMessage());
       }

    }
} 
